package com.sinosoft.perfect.system._.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public abstract class DateUtil {
	
	public static Date parseStringYYYY_MM_DDToDate(String dateString) {
		// YYYY-MM-DD
		String dateStrings[] = dateString.split("-");
		
		int year = Integer.valueOf(dateStrings[0]);
		// 比中国时间大一个月
		int month = Integer.valueOf(dateStrings[1]) - 1;
		int day = Integer.valueOf(dateStrings[2]);
		
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day, 0, 0, 0);
		
		return cal.getTime();
	}
	
	public static Date parseStringYYYY_MM_DDBlandHHMMToDate(String dateString) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date = null;
		try {
			if (dateString == null || "".equals(dateString)) {
				return new Date();
			}
			date = df.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static String formatDateToString(Date d ,String formatType) {
		if(d == null) return "";
		DateFormat df = new SimpleDateFormat(formatType);
		String newDate = df.format(d);
		return newDate;
	}
	
	public static String getNowYear() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = sdf.format(new Date());
		return date.substring(0, date.indexOf("-"));
	}
	
	public static Date getNowYearMonthDay() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowDate = sdf.format(new Date());
		Date date = null;
		try {
			date = sdf.parse(nowDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static void main(String[] args) {
		System.out.println(formatDateToString(new Date(),"yyyy-MM-dd"));
	}
}
